﻿using System;
using System.Collections.Generic;
using System.Data;
using PPI.Client.Logic.Distribution;
using PPI.Entities.Distribution;

namespace PPI.Controls.Grid
{
    public partial class GridDistributionReclosers : GridBase
    {
        private List<ERecloser> m_Reclosers;

        public List<ERecloser> CurrentReclosers
        {
            get
            {
                return m_Reclosers;
            }
        }

        public EDistributionPole Pole { get; set; }

        public GridDistributionReclosers()
        {
            InitializeComponent();
            cmiEditWhithOutImages.Visible = false;
        }

        public void LoadReclosers(string connectionString)
        {
            m_Reclosers = LRecloser.GetPoleReclosers(Pole, connectionString);
            LoadReclosers(m_Reclosers);
        }

        public void LoadReclosers(List<ERecloser> reclosers)
        {
            m_Reclosers = reclosers;
            RefreshGrid();
        }

        public void RefreshGrid()
        {
            var dt = new DataTable("Reclosers");

            dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Latitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Longitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Serial Number", typeof(string)));

            foreach (ERecloser recloser in m_Reclosers)
                dt.Rows.Add(recloser.Id, recloser.Latitude == double.MinValue ? 0 : recloser.Latitude, recloser.Longitude == double.MinValue ? 0 : recloser.Longitude, recloser.SerialNumber);

            datagridBase.AutoGenerateColumns = true;
            datagridBase.DataSource = dt;
            datagridBase.Refresh();
        }
    }
}
